package com.car.action.admin;

import java.util.List;
import java.util.Map;

import org.apache.commons.compress.utils.Lists;

import com.car.common.model.Product;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.utils.StringUtil;

public class ShiHouController extends Controller {

	public static List<String> typeList = Lists.newArrayList();
	public static List<String> styleList = Lists.newArrayList();
	public static List<String> spaceList = Lists.newArrayList();
	
	static {
		/*
		typeList.add("吸顶灯");
		typeList.add("吊灯");
		typeList.add("餐吊灯");
		typeList.add("壁灯镜前灯");
		typeList.add("台灯");
		typeList.add("落地灯");
		*/
		typeList.add("沙发类");
		typeList.add("厅柜类");
		typeList.add("书房类");
		typeList.add("卧室类");
		typeList.add("圆餐桌类");
		typeList.add("长方餐桌类");
		typeList.add("餐边柜类");
		typeList.add("酒柜类");
		typeList.add("中堂类");
		typeList.add("条几类");
		typeList.add("茶台类");
		typeList.add("茶水柜类");
		typeList.add("三件套椅几类");
		typeList.add("贵妃床类");
		typeList.add("罗汉床类");
		typeList.add("衣帽架类");
		typeList.add("花架类");
		typeList.add("多宝阁类");
		typeList.add("屏风挂匾类");
		typeList.add("麻将桌类");
		typeList.add("其他类");
		typeList.add("鞋柜类");
		typeList.add("家装饰品");
		
		styleList.add("现代简约");
		styleList.add("欧式风格");
		styleList.add("新中式风格");
		styleList.add("美式风格");
		styleList.add("田园风格");
		styleList.add("地中海风格");
		styleList.add("清式");
		styleList.add("明式");
		styleList.add("欧式");
		styleList.add("新中式");
		styleList.add("传统中式");
		styleList.add("轻奢风格");
		styleList.add("简美风格");
		
		spaceList.add("客厅大堂");
		spaceList.add("餐厅灯");
		spaceList.add("玄关及过道");
		/*
		spaceList.add("卧室灯");
		spaceList.add("书房灯");
		spaceList.add("浴室灯");
		spaceList.add("厨房灯");
		spaceList.add("阳台灯");
		*/
		spaceList.add("缅甸花梨");
		spaceList.add("老挝红酸枝");
		spaceList.add("老挝大红酸枝");
		spaceList.add("其他类");
		spaceList.add("儿童房");
	}
	
	
	public void banner() {
        render("banner.html");
	}
	
	public void index() {
        String sql = "select a.* , FROM_UNIXTIME(a.create_time/1000,'%Y-%m-%d %H:%i') as create_time_string  ";
    	String andSql = " from product as a where a.pic_url is not null " ;
    	String type = getPara("type");
		if(!StringUtil.empty(type)) {
			andSql  += " and type like '%"+ type+"%' ";
		}
		String style = getPara("style");
		if(!StringUtil.empty(style)) {
			andSql  += " and style like '%"+ style+"%' ";
		}
		String space = getPara("space");
		if(!StringUtil.empty(space)) {
			andSql  += " and space like '%"+ space+"%' ";
		}
		System.out.println(sql + andSql);
        List<Product> productList = Product.dao.find(sql + andSql);
        setAttr("productList", productList);
        setAttr("typeList", typeList);
		setAttr("styleList", styleList);
		setAttr("spaceList", spaceList);
		setAttr("type", type);
		setAttr("style", style);
		setAttr("space", space);
        render("index.html");
	}
	
	public void list() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        String sql = "select a.* , FROM_UNIXTIME(a.create_time/1000,'%Y-%m-%d %H:%i') as create_time_string  ";
    	String andSql = " from product as a order by a.id desc " ;
        
        Page<Product> productList = Product.dao.paginate(page, 20, sql, andSql);
        setAttr("page", productList);
        render("list.html");
	}
	
	public void shopping() {
		render("shopping.html");
	}
	
	public void introduce() {
		render("introduce.html");
	}
	
	public void one() {
		Product one = Product.dao.findById(getPara("id"));
		if (one == null) {
			one = new Product();
		}
		setAttr("one", one);
		setAttr("typeList", typeList);
		setAttr("styleList", styleList);
		setAttr("spaceList", spaceList);
		render("one.html");
	}
	
	public void cart(){
		Map<Integer,Product> map = getSessionAttr("cart");
		List<Map<String,Object>> result = Lists.newArrayList();
		if (map != null) {
			for(Integer key : map.keySet()) {
				Map<String,Object> temp = Maps.newHashMap();
				temp.put("id", map.get(key).get("id"));
				temp.put("name", map.get(key).get("name"));
				result.add(temp);
			}
		}
		renderJson(result);
	}
	
	
	public void add_cart(){
		Map<Integer,Product> map = getSessionAttr("cart");
		if (map == null) {
			map = Maps.newHashMap();
		}
		Integer id = getParaToInt("id");
		map.put(id, Product.dao.findById(id));
		setSessionAttr("cart", map);
		renderText("操作成功!");
		
	}
	
	public void detail() {
		Product one = Product.dao.findById(getPara("id"));
		if (one == null) {
			renderText("产品不存在或已下架!");
			return;
		}
		setAttr("one", one);
		setAttr("typeList", typeList);
		setAttr("styleList", styleList);
		setAttr("spaceList", spaceList);
		render("detail.html");
	}
	
	public void save() {
		Product one = Product.dao.findById(getPara("id"));
		boolean updateflg = true;
		if (one == null) {
			updateflg = false;
			one = new Product();
			one.set("create_time",  System.currentTimeMillis());
		}
		String type = getPara("type");
		String newType = "";
		for(String s : type.split(",")) {
			newType += "\"" + s + "\",";
		}
		if (newType.length() > 0) {
			newType = newType.substring(0, newType.length()-1);
		}
		one.set("type",      newType);
		String style = getPara("style");
		String newStyle = "";
		for(String s : style.split(",")) {
			newStyle += "\"" + s + "\",";
		}
		if (newStyle.length() > 0) {
			newStyle = newStyle.substring(0, newStyle.length()-1);
		}
		one.set("style",     newStyle);
		String space = getPara("space");
		String newSpace = "";
		for(String s : space.split(",")) {
			newSpace += "\"" + s + "\",";
		}
		if (newSpace.length() > 0) {
			newSpace = newSpace.substring(0, newSpace.length()-1);
		}
		one.set("space",     newSpace);
		one.set("name",      getPara("name"));
		one.set("pic_url",   getPara("pic_url"));
		one.set("size",      getPara("size"));
		one.set("quality",   getPara("quality"));
		one.set("price",     getPara("price"));
		
		if (updateflg) {
			one.update();
		} else {
			one.save();
		}
		renderText("操作成功!");
	}
}
